package com.wechatapp.mengyao.poetry.config;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.region.Region;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author yanghuaxu
 * @date 2025/7/5 17:50
 */
@Configuration
public class TencentCosConfig {

    @Value("${tencent.cos.secretId}")
    private String secretId;
    @Value("${tencent.cos.secretKey}")
    private String secretKey;
    @Value("${tencent.cos.socketTimeout}")
    private Integer socketTimeout;
    @Value("${tencent.cos.connectTimeout}")
    private Integer connectTimeout;
    @Value("${tencent.cos.region}")
    private String region;

    @Bean
    // 创建 COSClient 实例，这个实例用来后续调用请求
    public COSClient createCOSClient() {
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);

        // ClientConfig 中包含了后续请求 COS 的客户端设置：
        ClientConfig clientConfig = new ClientConfig();

        // 设置 bucket 的地域
        // COS_REGION 请参见 https://cloud.tencent.com/document/product/436/6224
        clientConfig.setRegion(new Region(region));

        // 以下的设置，是可选的：

        // 设置 socket 读取超时，默认 30s
        clientConfig.setSocketTimeout(socketTimeout * 1000);
        // 设置建立连接超时，默认 30s
        clientConfig.setConnectionTimeout(connectTimeout * 1000);

        // 生成 cos 客户端。
        return new COSClient(cred, clientConfig);
    }

}
